//
//  LCVideoWithListsViewController.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/20.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCVideoWithListsViewController.h"
#import "LCVideoHomeListAboutModel.h"

#define kIPhoneX ([UIScreen mainScreen].bounds.size.height == 812.0)

@interface LCVideoWithListsViewController ()
<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic) UITableView *tableView;

@end

@implementation LCVideoWithListsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"相关";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"LCAppraiseTableViewCell"];
    [self.tableView registerClass:[LSZSimilarVideoTableViewCell class] forCellReuseIdentifier:@"LSZSimilarVideoTableViewCellIdentifier"];

    self.tableView.tableFooterView = [[UIView alloc] init];
    if(@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            make.left.right.equalTo(self.view);
        }else{
            make.edges.equalTo(self.view);
        }
    }];
    [self getVideoWithList];
}
#pragma mark - 获取资源的找相似
-(void)getVideoWithList{
#pragma mark - 找相似的数据
    @weakify(self)
    [RecommendNetWork loadLCVideoWithListsViewControllerWithVideoId:self.id_id success:^(NSArray<Mp4Detail *> *arrayData) {
        @strongify(self)
        self.arrayData = arrayData;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"error = %@", error);
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 108;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSZSimilarVideoTableViewCell *cell;
    if (cell == nil) {
        cell =[[LSZSimilarVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"LSZSimilarVideoTableViewCellIdentifier"];
    }
    NSURL *url = [NSURL URLWithString:self.arrayData[indexPath.row].pic];
    [cell.imageViewCover sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

    cell.labelTitle.text = [NSString stringWithFormat:@"%@",self.arrayData[indexPath.row].name];
    cell.labelIntroduce.text = [NSString stringWithFormat:@"%@\n\n",self.arrayData[indexPath.row].content];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self FindSimilarClickEventsRowAtIndexPath:indexPath];//推荐的点击
}
#pragma mark - 找相似的点击事件
-(void)FindSimilarClickEventsRowAtIndexPath:(NSIndexPath *)indexPath {
    Mp4Detail *mp4DetailModel = self.arrayData[indexPath.row];
    NSDictionary *json = [mp4DetailModel yy_modelToJSONObject];
    _listAboutModel = [LCVideoHomeListAboutModel yy_modelWithJSON:json];

    LCVideoDetailChangeHeaderController *videoDetailChangeHeaderController = [[LCVideoDetailChangeHeaderController alloc] init];
    videoDetailChangeHeaderController.listAboutModel = _listAboutModel;
    [self.navigationController pushViewController:videoDetailChangeHeaderController animated:YES];

    //已经添加过了
    NSArray * arrayGetData = [[NSArray alloc]initWithContentsOfFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo]];
    for (NSDictionary * dic in arrayGetData) {
        NSString * name = NSStr(dic[@"id_id"]);
        if ([name isEqualToString:_listAboutModel.id]) {
            return;
        }
    }
    //还没有添加过
    NSMutableArray * mutArrayAdd = [[NSMutableArray alloc]initWithArray:arrayGetData];
    NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:_listAboutModel.name,@"title",_listAboutModel.pic,@"imageViewUrl",_listAboutModel.author,@"name",_listAboutModel.id,@"id_id",nil];
    [mutArrayAdd addObject:mutDic];
    [mutArrayAdd writeToFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo] atomically:YES];
}
- (UITableView *)tableView {
    if (!_tableView) {
        CGFloat H = kIPhoneX ? (self.view.bounds.size.height - 64 - 24 - 34) : self.view.bounds.size.height - 64;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, H) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}

@end
